﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nr.GLV.Windows.Controls
{
    public partial class FilterInputForm : Form
    {
        private List<FilterField> fields;
        private List<FilterOperator> operators;

        public IEnumerable<FilterField> Fields
        {
            get { return fields.AsEnumerable(); }
            set
            {
                fields = new List<FilterField>();
                cbFields.Items.Clear();
                if (value != null)
                {
                    fields.AddRange(value);
                    IEnumerable<string> items = from FilterField f in value select f.FriendlyName;
                    cbFields.Items.AddRange(items.ToArray());
                }
            }
        }

        public IEnumerable<FilterOperator> Operators
        {
            get { return operators.AsEnumerable(); }
            set
            {
                operators = new List<FilterOperator>();
                cbOperator.Items.Clear();
                if (value != null)
                {
                    operators.AddRange(value);
                    IEnumerable<string> items = from FilterOperator o in value select o.Text;
                    cbOperator.Items.AddRange(items.ToArray());
                    cbOperator.SelectedIndex = 0;
                }
            }
        }

        public int SelectedField
        {
            get { return cbFields.SelectedIndex; }
            set
            {
                try { cbFields.SelectedIndex = value; }
                finally { }
            }
        }

        public int SelectedOperator
        {
            get { return cbOperator.SelectedIndex; }
            set { cbOperator.SelectedIndex = value; }
        }

        public string Value { get { return txtValue.Text; } set { txtValue.Text = value; } }

        public FilterInputForm()
        {
            InitializeComponent();
        }

        public FilterInfo FilterInfo
        {
            get
            {
                return
                    new FilterInfo()
                        {
                            Field = Fields.ToArray()[cbFields.SelectedIndex],
                            Operator = Operators.ToArray()[cbOperator.SelectedIndex],
                            ValueToCompare = txtValue.Text
                        };
            }
        }

        private void FilterInputForm_Load(object sender, EventArgs e)
        {
            cbFields.SelectedIndex = 0;
            cbFields.Focus();
            cbOperator.SelectedIndex = 0;
        }
    }
}
